/*
 * Copyright (c)e 2024-2024.Powered by neflibata
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package com.liuzian.marketserver.service.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liuzian.marketserver.dto.R;
import com.liuzian.marketserver.entity.Commodity;
import com.liuzian.marketserver.entity.Orders;
import com.liuzian.marketserver.entity.User;
import com.liuzian.marketserver.mapper.OrdersMapper;
import com.liuzian.marketserver.service.CommodityService;
import com.liuzian.marketserver.service.OrdersService;
import com.liuzian.marketserver.service.UserService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class OrdersServiceImpl extends ServiceImpl<OrdersMapper, Orders> implements OrdersService {

    @Lazy
    @Resource
    private CommodityService commodityService;

    @Lazy
    @Resource
    private UserService userService;

    @Override
    public R<Orders> create(Orders orders) {
        orders.setCreateTime(new Date());
        LambdaQueryWrapper<Commodity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Commodity::getGid, orders.getGid());
        Commodity commodity = commodityService.getOne(queryWrapper);
        if (commodity.getCount() - orders.getCount() >= 0) {
            boolean bool = super.save(orders);
            LambdaUpdateWrapper<Commodity> commodityUpdateWrapper = new LambdaUpdateWrapper<>();
            commodityUpdateWrapper.eq(Commodity::getGid, orders.getGid());
            commodityUpdateWrapper.setSql("count = count -" + orders.getCount());
            commodityService.update(commodityUpdateWrapper);
            if (bool) {
                return R.success(orders);
            } else {
                return R.error("订单创建失败");
            }
        } else {
            return R.error("库存不足");
        }
    }

    @Override
    public R<String> pay(Long oid) {
        LambdaUpdateWrapper<Orders> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(Orders::getOid, oid);
        updateWrapper.set(Orders::getStatus, 2);
        updateWrapper.set(Orders::getExpressStatus, 1);
        Orders orders = super.getOne(updateWrapper);
        LambdaUpdateWrapper<User> userLambdaQueryWrapper = new LambdaUpdateWrapper<>();
        userLambdaQueryWrapper.eq(User::getUid, orders.getUid());
        userLambdaQueryWrapper.setSql("integral = integral -" + orders.getIntegral());
        userService.update(userLambdaQueryWrapper);
        boolean bool = super.update(updateWrapper);
        if (bool) {
            return R.success("支付成功");
        } else {
            return R.error("支付失败");
        }
    }

    @Override
    public R<String> cancel(Long oid) {
        LambdaQueryWrapper<Orders> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Orders::getOid, oid);
        Orders orders = super.getOne(wrapper);
        boolean bool = super.remove(wrapper);
        LambdaUpdateWrapper<Commodity> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.setSql("count = count +" + orders.getCount());
        commodityService.update(updateWrapper);
        if (bool) {
            return R.success("订单取消成功");
        } else {
            return R.error("订单取消失败");
        }
    }

    @Override
    public R<List<Orders>> paid(Long uid) {
        LambdaQueryWrapper<Orders> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Orders::getUid, uid);
        wrapper.eq(Orders::getStatus, 2);
        List<Orders> list = super.list(wrapper);
        LambdaQueryWrapper<Commodity> commodityWrapper;
        for (Orders order : list) {
            commodityWrapper = new LambdaQueryWrapper<>();
            commodityWrapper.eq(Commodity::getGid, order.getGid());
            order.setCommodity(commodityService.getOne(commodityWrapper));
        }
        return R.success(list);
    }

    @Override
    public R<List<Orders>> unPaid(Long uid) {
        LambdaQueryWrapper<Orders> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Orders::getUid, uid);
        wrapper.eq(Orders::getStatus, 1);
        List<Orders> list = super.list(wrapper);
        LambdaQueryWrapper<Commodity> commodityWrapper;
        for (Orders order : list) {
            commodityWrapper = new LambdaQueryWrapper<>();
            commodityWrapper.eq(Commodity::getGid, order.getGid());
            order.setCommodity(commodityService.getOne(commodityWrapper));
        }
        return R.success(list);
    }

    @Override
    public R<List<Orders>> transitOrder(Long uid) {
        LambdaQueryWrapper<Orders> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Orders::getUid, uid);
        wrapper.eq(Orders::getExpressStatus, 1);
        List<Orders> list = super.list(wrapper);
        LambdaQueryWrapper<Commodity> commodityWrapper;
        for (Orders order : list) {
            commodityWrapper = new LambdaQueryWrapper<>();
            commodityWrapper.eq(Commodity::getGid, order.getGid());
            order.setCommodity(commodityService.getOne(commodityWrapper));
        }
        return R.success(list);
    }

    @Override
    public R<List<Orders>> receiveOrder(Long uid) {
        LambdaQueryWrapper<Orders> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Orders::getUid, uid);
        wrapper.eq(Orders::getExpressStatus, 2);
        wrapper.orderByDesc(Orders::getReceiveTime);
        List<Orders> list = super.list(wrapper);
        LambdaQueryWrapper<Commodity> commodityWrapper;
        for (Orders order : list) {
            commodityWrapper = new LambdaQueryWrapper<>();
            commodityWrapper.eq(Commodity::getGid, order.getGid());
            order.setCommodity(commodityService.getOne(commodityWrapper));
        }
        return R.success(list);
    }

    @Override
    public R<String> finishOrder(Long oid) {
        LambdaUpdateWrapper<Orders> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(Orders::getOid, oid);
        wrapper.set(Orders::getExpressStatus, 2);
        wrapper.set(Orders::getReceiveTime, new Date());
        boolean bool = super.update(wrapper);
        if (bool) {
            Orders orders = super.getOne(wrapper);
            LambdaUpdateWrapper<User> userLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
            userLambdaUpdateWrapper.eq(User::getUid, orders.getUid());
            userLambdaUpdateWrapper.setSql("integral = integral +" + orders.getPrice());
            userService.update(userLambdaUpdateWrapper);
            return R.success("商品签收成功");
        } else {
            return R.error("商品签收失败");
        }
    }
}
